package com.haisen.controller;

import com.alibaba.fastjson.JSON;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.RegexUtil;
import com.haisen.core.utils.Result;
import com.haisen.dataobject.entity.BussItem;
import com.haisen.dataobject.entity.User;
import com.haisen.dataobject.query.BussItemQuery;
import com.haisen.exception.BizException;
import com.haisen.service.BussItemService;
import com.haisen.service.DicService;
import com.haisen.service.OrganizationService;
import com.haisen.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/produce")
@Slf4j
public class BussItemController {

    @Autowired
    private BussItemService bussItemService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrganizationService organizationService;

    //数据字典
    @Autowired
    private DicService dicService;

    @GetMapping
    @RequiresPermissions("produce:view")
    public String page(Model model) {
        setCommonDate(model);
        return "produce/produce";
    }

    @ResponseBody
    @RequiresPermissions("produce:view")
    @RequestMapping("/list")
    public PageResultSet<BussItem> list(BussItemQuery bussItemQuery) {
       return bussItemService.findByPage(bussItemQuery);
    }

    @ResponseBody
    @RequiresPermissions("produce:create")
    @PostMapping("/create")
    public Result create(@Validated BussItem bussItem) {
      /*  if(br.hasErrors()){
            return Result.failure(br);
        }*/
        bussItemService.create(bussItem);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("produce:update")
    @PostMapping("/update")
    public Result update(@Validated BussItem bussItem) {
        bussItemService.update(bussItem);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("produce:delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long[] ids) {
        bussItemService.delete(ids);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("produce:import")
    @PostMapping("/import")
    public Result importExcel(@RequestParam("importpathfile") String pathFile) {
        log.info("导入文件：", pathFile);
        bussItemService.importExcel2DataBase(pathFile);
        return Result.success();
    }

    /**
     * 审核生产明细
     */
    @ResponseBody
    //@RequiresPermissions("produce:verify")
    @PostMapping("/verify")
    public Result verifyDate(@RequestParam("status") String status, @RequestParam("id") Long[] ids) {
        if (!RegexUtil.isNumber(status)) throw new BizException("审核标记状态必须为数字!标记为：" + status);
        bussItemService.verifyDate(Integer.valueOf(status), ids);
        return Result.success();
    }

    /**
     * 通过ajax获取导入状态，前台显示进度条
     * @return
     * 换成websocket方式，取消掉
     * todo:/websocket改成点点对方是进行，否则都能收到
     */
    /*
    @ResponseBody
    @GetMapping("/status")
    public Result<Integer> importStatus(){
        Integer insertedNumber = InsertBatch.insertedNumer;
        Integer lineNumber = InsertBatch.lineNumber;
        Integer complateRate =0;
        if (lineNumber !=0 ){
            complateRate = 100*insertedNumber/lineNumber;
        }
        return Result.success(complateRate);
    }
  */

    /**
     * 将变量传递到model中attribute
     *
     * @param model
     */
    private void setCommonDate(Model model) {
        model.addAttribute("bizTypeMap", dicService.GetDic("tb_buss_item", "biz_type"));
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        log.info("用户名：{}", username);
        model.addAttribute("user", username);
    }


}
